package com.example.donghuihui.androidframe.utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.DisplayMetrics;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 基本工具类
 */
public class BaseUtil {
    /**
     * 获取屏幕宽度
     * @param activity 当前activity上下文对象
     * @return 屏幕宽度
     */
    public static int getScreenWidth(Activity activity) {
        if (activity == null){
            return 0;
        }
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }

    /**
     * 得到设备屏幕的宽度
     * @param context 上下文
     */
    public static int getScreenWidth(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels;
    }

    /**
     * 得到设备屏幕的高度
     * @param context 上下文
     */
    public static int getScreenHeight(Context context) {
        return context.getResources().getDisplayMetrics().heightPixels;
    }


    /**
     * 判断是不是手机号(这里只限定1开头的11位数字就认为是手机号)
     * @param mobiles
     * @return
     */
    public static boolean isMobileNO(String mobiles){

        Pattern p = Pattern.compile("^((1[0-9]))\\d{9}$");

        Matcher m = p.matcher(mobiles);

        return m.matches();

    }

    /**
     * 判断邮箱格式是否正确
     * @param email
     * @return
     */
    public static boolean isEmail(String email){
        Pattern p = Pattern.compile("^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$");
        Matcher m = p.matcher(email);
        return m.matches();
    }

    /**
     * 判断身份证号格式
     * @param IDCard
     * @return
     */
    public static boolean isIDCard(String IDCard){
        Pattern p = Pattern.compile("(^\\d{18}$)|(^\\d{15}$)");
        Matcher m = p.matcher(IDCard);
        return m.matches();
    }

    /**
     * 过滤HTML特殊符号
     * @param htmldata 需要转码的html语言
     * @return
     */
    public static String changeHTML(String htmldata) {
        htmldata = htmldata.replaceAll("&amp;", "&");
        htmldata = htmldata.replaceAll("\\\\&quot;", "\"");
//		htmldata = htmldata.replaceAll("&quot;", "\"");
        htmldata = htmldata.replaceAll("&#039;", "'");
        htmldata = htmldata.replaceAll("&lt;", "<");
        htmldata = htmldata.replaceAll("&gt;", ">");
        htmldata = htmldata.replaceAll("&amp;nbsp;", " ");
        return htmldata;
    }

    /**
     * 设置价钱显示精确到小数点后两位
     * @param price
     * @return
     */
    public static String priceFormat(double price){
        DecimalFormat df   = new DecimalFormat("######0.00");
        return df.format(price);
    }

    /**
     * DP转px
     * @param context
     * @param dip
     * @return
     */
    public static int dipToPixels(Context context, int dip) {
        final float SCALE = context.getResources().getDisplayMetrics().density;
        float valueDips = dip;
        int valuePixels = (int) (valueDips * SCALE + 0.5f);
        return valuePixels;
    }

    /**
     *
     * @return 返回当前月份的最大天数
     */
    public static int getMaxNumforMonth(int year,int month){
        Calendar time= Calendar.getInstance();
        time.clear();
        time.set(Calendar.YEAR,year);
        time.set(Calendar.MONTH,month-1);//注意,Calendar对象默认一月为0
        return time.getActualMaximum(Calendar.DAY_OF_MONTH);//本月份的天数
    }

    /**
     * 判断apk是否完整
     *
     * @param context
     * @param path
     * @return
     */
    public static boolean isApkIntegrity(Context context, String path) {
        boolean result = false;
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo info = pm.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);
            if (info != null) {
                result = true;
            } else {
                result = false;
            }
        } catch (Exception e) {
            result = false;
        }
        return result;
    }
}
